﻿Public Class AppStartup
    Dim siteDao As SiteDAO = New SiteDAO()
    Dim sites As List(Of Site) = siteDao.getAllSite()
    Private Sub AppStartup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        selectedSiteBox.DisplayMember = "NOMSITE"
        allSiteBox.DataSource = sites
        allSiteBox.DisplayMember = "NOMSITE"
        allSiteBox.Refresh()
        selectedSiteBox.Refresh()
        If Settings.Default.DisplayedSitesOnStartup Is Nothing OrElse Settings.Default.DisplayedSitesOnStartup.Trim().Length = 0 Then
            Return
        End If

        Dim tokens As String() = Settings.Default.DisplayedSitesOnStartup.Split(";")
        Dim listToBeRemoved As New List(Of Site)
        For Each site As Site In sites
            If tokens.Contains(site.CODSITE) Then
                listToBeRemoved.Add(site)
                selectedSiteBox.Items.Add(site)
            End If
        Next
        For Each site As Site In listToBeRemoved
            sites.Remove(site)
        Next
        'allSiteBox.Items().Clear()
        allSiteBox.DataSource = Nothing
        allSiteBox.DataSource = sites
        allSiteBox.DisplayMember = "NOMSITE"
        allSiteBox.Refresh()
        selectedSiteBox.Refresh()
    End Sub

    Private Sub btnMoveRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveRight.Click
        If allSiteBox.SelectedItem Is Nothing Then
            Return
        End If

        Dim site As Site = allSiteBox.SelectedItem
        sites.Remove(site)
        selectedSiteBox.Items().Add(site)
        'allSiteBox.Items().Clear()
        allSiteBox.DataSource = Nothing
        allSiteBox.DataSource = sites
        allSiteBox.DisplayMember = "NOMSITE"
        allSiteBox.Refresh()
        selectedSiteBox.Refresh()
    End Sub

    Private Sub btnMoveLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveLeft.Click
        If selectedSiteBox.SelectedItem Is Nothing Then
            Return
        End If

        Dim site As Site = selectedSiteBox.SelectedItem
        selectedSiteBox.Items().Remove(site)
        sites.Add(site)
        'allSiteBox.Items().Clear()
        allSiteBox.DataSource = Nothing
        allSiteBox.DataSource = sites
        allSiteBox.DisplayMember = "NOMSITE"
        allSiteBox.Refresh()
        selectedSiteBox.Refresh()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub btnValider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValider.Click
        Dim selectedSiteStr As String = ""
        For Each site As Site In selectedSiteBox.Items()
            selectedSiteStr = selectedSiteStr & ";" & site.CODSITE
        Next
        If selectedSiteStr.Length > 0 Then
            selectedSiteStr = selectedSiteStr.Substring(1)
        End If

        Settings.Default.DisplayedSitesOnStartup = selectedSiteStr
        Settings.Default.Save()
        Settings.[Default].Reload()
        Me.Dispose()
    End Sub
End Class